<template>
  <div class="user">
    <div class="userBox">
      <div class="userBox-top">
        <h1>员工管理</h1>
        <button @click="addStaff">添加员工</button>
      </div>
      <div class="table">
        <div class="table-top">
          <h2>员工信息</h2>
          <div>
            <input type="text" v-model="queryinfo.staffName" placeholder="请输入员工名称" />
            <button @click="searchStaff">搜索</button>
          </div>
        </div>
        <table>
          <tbody>
            <tr>
              <th>序号</th>
              <th>宇宙id</th>
              <th>用户名</th>
              <th>密码</th>
              <th>性别</th>
              <th>薪水</th>
              <th>操作</th>
            </tr>
            <tr v-for="(item,index) in staffList" :key="item.id">
              <th>{{index+1}}</th>
              <th>{{item._id}}</th>
              <th>{{item.username}}</th>
              <th>{{item.password}}</th>
              <th>{{item.sex}}</th>
              <th>{{item.salary}}</th>
              <th>
                <button style="backgroundColor:yellow" @click="updateUser(item._id)">修改</button>
                <button style="backgroundColor:red" @click="deleteUser(item._id)">删除</button>
              </th>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "user",
  props: [],
  components: {},
  data() {
    return {
      staffList: [],
      queryinfo: {
        id: "",
        staffName: ""
      }
    };
  },
  //方法 函数写这里
  methods: {
    async getStaffList() {
      const res = await this.$http.get("http://127.0.0.1:3003/staff");
      this.staffList = res.data.data;
    },
    async searchStaff() {
      if (this.queryinfo.staffName == "") {
        return;
      }
      const res = await this.$http.get("http://127.0.0.1:3003/searchStaff", {
        params: this.queryinfo
      });
      this.staffList = res.data.data;
    },
    addStaff() {
      this.$router.push("/home/addStaff");
    },
    updateStaff() {},
    async deleteUser(id) {
      var a = confirm("操作将永久删除员工确定吗?");
      if (!a) {
        this.$message.info("算了先不删");
        return;
      }
      const res = await this.$http.get("http://127.0.0.1:3003/deleteStaff", {
        params: id
      });
      this.userList = res.data;
      this.getStaffList();
    }
  },
  //计算属性
  computed: {},
  //侦听器
  watch: {},
  //过滤器
  filters: {},
  //以下是生命周期
  //组件创建之前
  beforeCreate() {},
  //组件创建之后
  created() {
    this.getStaffList();
  },
  //页面渲染之前
  beforeMount() {},
  //页面渲染之后
  mounted() {},
  //页面视图数据更新之前
  beforeUpdate() {},
  //页面视图数据更新之后
  updated() {},
  //页面销毁之前
  beforeDestroy() {},
  //页面销毁之后
  destroyed() {}
};
</script>


<style lang="scss"  scoped>
.user {
  width: 100%;
  height: 100%;
}
.userBox {
  margin-top: 10px;
  margin-left: 10px;
  h1 {
    display: inline-block;
    margin-right: 10px;
  }
  .userBox-top button {
    width: 80px;
    height: 24px;
    line-height: 24px;
    background-color: skyblue;
  }
}
.table {
  width: 100%;
  margin-top: 20px;
  background-color: white;
  box-shadow: 1px -1px black;
  padding-left: 5px;
  .table-top {
    height: 60px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    button {
      background-color: skyblue;
      margin-right: 400px;
    }
  }
  tbody tr {
    margin-top: 10px;
    font-size: 16px;
    th {
      padding: 10px 20px;
      button {
        width: 60px;
        height: 30px;
      }
    }
  }
}
</style>